/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package stuff;

import GUI.NewVisit;
import java.awt.Component;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import java.util.TreeMap;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.EventListenerList;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import model.Diagnose;

/**
 *
 * @author JArik
 */
public class TableButtonEditor extends JButton implements TableCellEditor {

    protected EventListenerList listenerList = new EventListenerList();
    protected ChangeEvent changeEvent = new ChangeEvent(this);
    private JTable diagTable;
    private TreeMap pressed;
    private Object mValue;
    public TableButtonEditor(String name,JTable table,TreeMap press) {
        super();
        this.setText(name);
        diagTable = table;

        pressed = press;
        this.setMargin(new Insets(3,1,3,1));
        addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                fireEditingStopped();
            }
        });
    }
    public TableButtonEditor(String name) {
        super();
        this.setText(name);
        
        this.setMargin(new Insets(3,1,3,1));
        final TableButtonEditor editor = this;
        /*addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                editor.stopCellEditing();
            }
        });*/
    }
    public void addCellEditorListener(CellEditorListener listener) {
        listenerList.add(CellEditorListener.class, listener);
    }

    public void removeCellEditorListener(CellEditorListener listener) {
        listenerList.remove(CellEditorListener.class, listener);
    }

    protected void fireEditingStopped() {
        CellEditorListener listener;
        Object[] listeners = listenerList.getListenerList();
        
        for (int i = 0; i < listeners.length; i++) {
            if (listeners[i] == CellEditorListener.class) {
                if(listeners.length <= i+1) return;
                listener = (CellEditorListener) listeners[i+1];
                listener.editingStopped(changeEvent);
            }
        }
    }

    protected void fireEditingCanceled() {
        CellEditorListener listener;
        Object[] listeners = listenerList.getListenerList();
        for (int i = 0; i < listeners.length; i++) {
            if (listeners[i] == CellEditorListener.class) {
                if(listeners.length <= i+1) return;
                listener = (CellEditorListener) listeners[i+1];
                listener.editingCanceled(changeEvent);
            }
        }
    }

    //@Override
    public void cancelCellEditing() {
        fireEditingCanceled();
    }

    public boolean stopCellEditing() {
        fireEditingStopped();
        return true;
    }

    public boolean isCellEditable(EventObject event) {
        return true;
    }

    public boolean shouldSelectCell(EventObject event) {
        return true;
    }

    @Override
    public Object getCellEditorValue() {
        this.setSelected(false);
        return mValue;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) {

        mValue = value;
        if(value != null && pressed != null)
        if((Boolean)pressed.get(value.toString()) == false && ((Diagnose)value).isPredv())
        {
          this.setEnabled(true);
          this.setSelected(true);
          //DefaultTableModel model = (DefaultTableModel)diagTable.getModel();
          Diagnose diag = (Diagnose)value;
          // ppc
          ((NewVisit)table.getRootPane().getParent()).addToDiagTable(diag,true);
          pressed.put(value.toString(), true);
        } else
        {
            this.setEnabled(false);
            this.setSelected(false);
        }
        //System.out.println("Editor-Enb: "+this.isEnabled());
        return this;
    }

}
